<?php 
/**
* 
*/
class SalasController extends Sgit_Controller_Action
{

    protected $_main_model='Salas';
    protected $_main_form='Salas';
    protected $_checkZona = TRUE;
    
    function _begin()
    {
        $this->_file_upload[1] = Sgit_Util_FileUpload::run('sala','vacia_'.$this->_idZona);
        $this->_file_upload[2] = Sgit_Util_FileUpload::run('sala','actual_'.$this->_idZona);
        


    }
    

    public function informeAction()
    {
        
        $id_sala=$this->_getParam('id_sala');
        
        
        if (!$id_sala)
        {
            throw new Zend_Controller_Exception(sprintf('The param id_sala is required  %s', $this->_controllerName . ':' . $this->_actionName));
        }
        
        $where1['id=?']=$id_sala;
        $where1['id_zona=?']=$this->_idZona;
        $this->view->sala= $this->_model->default->getRow($where1);


        if (!$this->view->sala)
        {
            throw new Zend_Controller_Exception(sprintf('This id_sala not exits for this id_zona %s', $this->_controllerName . ':' . $this->_actionName));
        }

        $m_proyectos=new Application_Model_Proyectos();
        
        $where['id_sala=?']=$id_sala;
        $where['activo=?']=1;
        $proyectos = $m_proyectos->getAll($where);
        foreach($proyectos as &$v){
            
            $v['racks_total']=$v['racks_libres']+$v['racks_instalados'];
            
        }
        
        $this->view->suma=Sgit_Util_Suma::run($proyectos,array('sup_ocupada','racks_total','racks_libres','racks_instalados'));
        
        $ocupacion['total']['value']=$this->view->sala['sup_neta'];
        $ocupacion['clientes']['value']=$this->view->suma['sup_ocupada'];
        $ocupacion['servidumbre']['value']=round($this->view->suma['sup_perdida'],2);
        $ocupacion['libre']['value']=$ocupacion['total']['value']-$ocupacion['servidumbre']['value']-$ocupacion['clientes']['value'];
        
        $ocupacion['total']['porcentaje']=100;
        $ocupacion['clientes']['porcentaje']=(100/$ocupacion['total']['value'])*$ocupacion['clientes']['value'];
        $ocupacion['servidumbre']['porcentaje']=(100/$ocupacion['total']['value'])*$ocupacion['servidumbre']['value'];
        $ocupacion['libre']['porcentaje']=(100/$ocupacion['total']['value'])*$ocupacion['libre']['value'];
        
        $ocupacion['ocupado']['value']=$ocupacion['clientes']['value']+$ocupacion['servidumbre']['value'];
        
        
        $racks['total']['value']=$this->view->suma['racks_total'];
        $racks['instalados']['value']=$this->view->suma['racks_instalados'];
        $racks['libres']['value']=$this->view->suma['racks_libres'];
        
        $racks['total']['porcentaje']=100;
        $racks['instalados']['porcentaje']=(100/$racks['total']['value'])*$racks['instalados']['value'];
        $racks['libres']['porcentaje']=(100/$racks['total']['value'])*$racks['libres']['value'];
        

        $this->view->racks=$racks;
        $this->view->ocupacion=$ocupacion;
        $this->view->proyectos=$proyectos;


    }
    
    public function imputacionAction()
    {
        $m_salas=new Application_Model_InformeElectricidadMensualSalas();
         
        $filtros = new Sgit_Form_Filtros();
        
        
        $anio = new Sgit_Form_Element_Select_Year('anio');
        $mes = new Sgit_Form_Element_Select_Mes('mes');
        $salas= new Sgit_Form_Element_Select_Salas('id_sala');
        
        $filtros->addFormSelect($anio);
        $filtros->addFormSelect($mes);
        $filtros->addFormSelect($salas);

        $values = $this->_getFiltrosValues($filtros);
        $filtros->addPdf();
        $filtros->addExcel();
        $filtros->myRender();
        
        $values['id_zona']=$this->_idZona;
         
        $this->view->rows= $m_salas->getListadoImputacion($values);

        $this->view->suma = Sgit_Util_Suma::run($this->view->rows, array('consumo', 'potencia', 'superficie_neta','superficie_ocupada','superficie_contratada','superficie_perdida'));

        /*
        $this->_pdfSubTitle = $this->zona;
        $this->_export($this->view->rows);
        
        $xml = $this->_getXml($this->_actionName);
        $this->view->columnas = $xml['columnas'];
        */
         
    }
      

    public function indexAction()
    {
      
        $filtros = new Sgit_Form_Filtros();

        $f_mes = new Sgit_Form_Element_Select_Ubicacion('ubicacion');
        $f_mes->setLabel('Ubicación');
        $filtros->addFormSelect($f_mes);

        $element=new Sgit_Form_Element_Select_Xml('TipoSala');
        $filtros->addFormSelect($element);
         
        $element=new Sgit_Form_Element_Select_Xml('EstadoSala');
        $filtros->addFormSelect($element);


        $filtros->addExcel();
        $filtros->addPdf();
        $filtros->myRender();

        $values = $this->_getFiltrosValues($filtros);

        if($values['tipo_sala'])
            $where['tipo_sala = ?']=$values['tipo_sala'];
        if($values['estado'])
            $where['estado = ?']=$values['estado'];
        if($values['ubicacion'])
            $where['ubicacion = ?']=$values['ubicacion'];
        

        $this->_element->TipoSistema=new  Sgit_Form_Element_Select_Xml('TipoSistema');
        $rows = $this->_model->default->getAllByZona($this->_idZona,$where);
        $this->view->rows =$this->_prepareRowsToTr($rows);
        $this->view->suma = Sgit_Util_Suma::run($this->view->rows, array('sup_neta', 'nclimas', 'potencia_frio'));

        $this->_pdfSubTitle = $this->zona;
        $this->_export($this->view->rows);
        
        $xml = $this->_getXml($this->_actionName);
        $this->view->columnas = $xml['columnas'];
        
        
    }
	
    public function editarAction()
    {
        $referer = $this->getRequest()->getServer('HTTP_REFERER');
        if(strpos($referer,'informe')){
            $this->_reload();
        }

        
        $this->_editar();
    }
    
    public function insertarAction()
    {
        $this->_insertar();
    }
    
    public function borrarAction()
    {
        $this->_borrar();
    }
    
    public function _prepareValuesToTr($row)
    {
       $row['f_update'] =    Sgit_Filter_DateDbToLocalize::filter($row['f_update']);
       return $row;
    }
    
    protected function _prepareValuesToForm($row)
    {
        
        $row['f_update']=$row['f_update'] ? $row['f_update'] : date('Y-m-d');
        $row['f_update'] = Sgit_Filter_DateDbToForm::filter($row['f_update']);
        return $row;
    }


    protected function _prepareValuesToDb()
    {
        
        $row=$this->_form->getValues();
        $row['id_zona']=$this->_idZona;
        
        $row['f_update'] = Sgit_Filter_DateToDb::filter($row['f_update']);
        
        Sgit_Util_removeFiles::run($this->_file_upload[1]['tabla'],$this->_file_upload[1]['key'],$this->_id,$row[$this->_file_upload[1]['activo']]);
        unset($row[$this->_file_upload[1]['name']]);
        unset($row[$this->_file_upload[1]['activo']]);
        
        Sgit_Util_removeFiles::run($this->_file_upload[2]['tabla'],$this->_file_upload[2]['key'],$this->_id,$row[$this->_file_upload[2]['activo']]);
        unset($row[$this->_file_upload[2]['name']]);
        unset($row[$this->_file_upload[2]['activo']]);
        
        return $row;
    }


	
}


